package com.tz.service;

import com.tz.entity.ZhangWu;
import com.tz.mapper.ZhangWuMapper;
import com.tz.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class ZhangWuService {
    private SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有
     * @return
     */
    public List<ZhangWu> selectAll(){
        //调用ZhangWuMapper.selectAll()

        //2.获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取ZhangWuMapper
        ZhangWuMapper mapper = sqlSession.getMapper(ZhangWuMapper.class);
        //4.调用方法
        List<ZhangWu> zhangWus = mapper.selectAll();
        //5.关闭资源
        sqlSession.close();
        return zhangWus;
    }

    public void add(ZhangWu zhangWu){

        //2.获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取ZhangWuMapper
        ZhangWuMapper mapper = sqlSession.getMapper(ZhangWuMapper.class);
        //4.调用方法
        mapper.add(zhangWu);
        //5.提交事务
        sqlSession.commit();
        //5.关闭资源
        sqlSession.close();
    }
}
